package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.response.OperateBookResponse;
import com.yanchangapp.model.response.PlanDetailResponse;
import com.yanchangapp.ui.view.OperateBookAtView;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-10-10.
 * description:作业指导书p层
 */
public class OperateBookAtPresenter extends BasePresenter<OperateBookAtView> {

    public OperateBookAtPresenter(BaseActivity context) {
        super(context);
    }



    public void queryPlanDetail(long planID){
        HeadApiRetrofit.getInstance().getQueryPlanDetail(planID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<PlanDetailResponse>>() {
                    @Override
                    public void onNext(BaseResponse<PlanDetailResponse> planDetailResponseBaseResponse) {
                        if (null != getView()){
                            if (AppConstant.REQUEST_SUCCESS == planDetailResponseBaseResponse.getCode()){
                                getView().getOperateDetailSuccess(planDetailResponseBaseResponse.getData());
                            }else {
                                getView().getOperateDetailFailure(planDetailResponseBaseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().getOperateDetailFailure("网络错误");
                        }
                    }
                });
    }

    public void startPlan(long planID){
        if (getView() != null){
            getView().showLoadingDialog();
        }

        HeadApiRetrofit.getInstance().putStartPlan(planID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                                getView().startPlanSuccess("已开始");
                            }else {
                                getView().startPlanFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            getView().startPlanFailure("网络错误");
                        }
                    }
                });

    }


}
